package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcLogisticsTrack;
import com.ruoyi.fun.service.IEcLogisticsTrackService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物流轨迹Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/track")
public class EcLogisticsTrackController extends BaseController
{
    @Autowired
    private IEcLogisticsTrackService ecLogisticsTrackService;

    /**
     * 查询物流轨迹列表
     */
    @PreAuthorize("@ss.hasPermi('fun:track:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcLogisticsTrack ecLogisticsTrack)
    {
        startPage();
        List<EcLogisticsTrack> list = ecLogisticsTrackService.selectEcLogisticsTrackList(ecLogisticsTrack);
        return getDataTable(list);
    }

    /**
     * 导出物流轨迹列表
     */
    @PreAuthorize("@ss.hasPermi('fun:track:export')")
    @Log(title = "物流轨迹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcLogisticsTrack ecLogisticsTrack)
    {
        List<EcLogisticsTrack> list = ecLogisticsTrackService.selectEcLogisticsTrackList(ecLogisticsTrack);
        ExcelUtil<EcLogisticsTrack> util = new ExcelUtil<EcLogisticsTrack>(EcLogisticsTrack.class);
        util.exportExcel(response, list, "物流轨迹数据");
    }

    /**
     * 获取物流轨迹详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:logisticsTrack:query')")
    @GetMapping(value = "/{trackId}")
    public AjaxResult getInfo(@PathVariable("trackId") Long trackId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecLogisticsTrackService.selectEcLogisticsTrackByTrackIdAndOrgId(trackId, orgId));
    }

    /**
     * 新增物流轨迹
     */
    @PreAuthorize("@ss.hasPermi('fun:track:add')")
    @Log(title = "物流轨迹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcLogisticsTrack ecLogisticsTrack)
    {
        ecLogisticsTrack.setOrgId(getOrgId());
        return toAjax(ecLogisticsTrackService.insertEcLogisticsTrack(ecLogisticsTrack));
    }

    /**
     * 修改物流轨迹
     */
    @PreAuthorize("@ss.hasPermi('fun:track:edit')")
    @Log(title = "物流轨迹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcLogisticsTrack ecLogisticsTrack)
    {
        return toAjax(ecLogisticsTrackService.updateEcLogisticsTrack(ecLogisticsTrack));
    }

    /**
     * 删除物流轨迹
     */
    @PreAuthorize("@ss.hasPermi('fun:track:remove')")
    @Log(title = "物流轨迹", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trackIds}")
    public AjaxResult remove(@PathVariable Long[] trackIds)
    {
        return toAjax(ecLogisticsTrackService.deleteEcLogisticsTrackByTrackIds(trackIds));
    }
    /**
     * 根据trackId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:eclogisticstrack:query')")
    @GetMapping(value = "/{trackId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("trackId") Long trackId, @PathVariable("orgId") Long orgId)
    {
        return success(ecLogisticsTrackService.selectEcLogisticsTrackByTrackIdAndOrgId(trackId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:eclogisticstrack:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcLogisticsTrack ecLogisticstrack = new EcLogisticsTrack();
        List<EcLogisticsTrack> list = ecLogisticsTrackService.selectEcLogisticsTrackListByOrgId(ecLogisticstrack, orgId);
        return getDataTable(list);
    }


}

